<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 * 余额支付处理
 */
 
namespace osc\payment\controller;
use osc\common\controller\Base;
use think\Db;
class Predeposit extends Base{
	
	
	public function process(){
		
		$input=input('param.');
				
		
		$uid=isset($input['uid'])?$input['uid']:member('uid');
		$type=isset($input['type'])?$input['type']:config('common_goods');
		$shipping_method=isset($input['shipping_method'])?$input['shipping_method']:session('shipping_method');
		$shipping_city_id=isset($input['shipping_city_id'])?$input['shipping_city_id']:session('shipping_city_id');		
		$shipping_address_id=isset($input['shipping_address_id'])?$input['shipping_address_id']:(int)session('shipping_address_id');
		$comment=isset($input['comment'])?$input['comment']:(int)session('comment');
		$username=isset($input['username'])?$input['username']:member('username');
		$predeposit=isset($input['predeposit'])?$input['predeposit']:member('predeposit');
		
		$post['uid']=$uid;
		$post['type']=$type;
		$post['shipping_method']=$shipping_method;
		$post['city_id']=$shipping_city_id;
		$post['predeposit']=$predeposit;
		$post['address_id']=$shipping_address_id;
		$post['comment']=$comment;		
		$post['username']=$username;
		
		$api=osc_service('common','Predeposit');

		$r=$api->pay($post);

		if(isset($r['error'])){
			return array('error'=>$r['error']);
		}else{
			return array('url'=>url('/pay_success'));
		}
		
	}

	public function re_pay($order_id){

		$order=Db::name('order')->where('order_id',(int)$order_id)->find();
		
		osc_order()->update_order($order['order_id']);
			
		$username=member('username');
		
		$username=empty($username)?member('nickname'):$username;
		
		osc_service('common','Predeposit')->update_predeposit(array('uid'=>$order['uid'],'username'=>$username,'order_id'=>$order['order_id'],'order_num_alias'=>$order['order_num_alias'],'total'=>$order['total']));
		
							
		return array('type'=>'predeposit','pay_url'=>url('/pay_success'));
	}
}
